package com.ruoyi.web.controller.system;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.domain.ViewStatus;
import com.ruoyi.system.service.IViewStatusService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.CallBoard;
import com.ruoyi.system.service.ICallBoardService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 公告板管理Controller
 * 
 * @author cyb
 * @date 2021-08-07
 */
@Controller
@RequestMapping("/system/board")
public class CallBoardController extends BaseController
{
    private String prefix = "system/board/";

    @Autowired
    private IViewStatusService viewStatusService;
    @Autowired
    private ICallBoardService callBoardService;


    @GetMapping()
    public String board()
    {
        return prefix + "board";
    }

    /**
     * 查询公告板管理列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CallBoard callBoard)
    {
        startPage();
        List<CallBoard> list = callBoardService.selectCallBoardList(callBoard);
        return getDataTable(list);
    }

    /**
     * 导出公告板管理列表
     */
    @Log(title = "公告板管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CallBoard callBoard)
    {
        List<CallBoard> list = callBoardService.selectCallBoardList(callBoard);
        ExcelUtil<CallBoard> util = new ExcelUtil<CallBoard>(CallBoard.class);
        return util.exportExcel(list, "公告板管理数据");
    }

    /**
    * 公告板展示
    * */
    @GetMapping("/callboard")
    public String callboard(Model model){
        List<CallBoard>callboards=callBoardService.selectCallBoardByStatus("Y");
        model.addAttribute("callboards",callboards);
        return "system/board/callboard";
    }
    /**
     * 新增公告板管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存公告板管理
     */
    @Log(title = "公告板管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CallBoard callBoard)
    {
        SimpleDateFormat sdf=new SimpleDateFormat("YYYY年MM月dd日");
        String date=sdf.format(new Date());
        callBoard.setCreatTime(date);
        callBoard.setSticktime(date);
        SysUser sysUser=(SysUser) SecurityUtils.getSubject().getPrincipal();
        callBoard.setCreater(sysUser.getUserName());
        callBoard.setViewCount((long)0);
        return toAjax(callBoardService.insertCallBoard(callBoard));
    }

    /**
     * 浏览量增加
     */
    @PostMapping("/viewstatus")
    @ResponseBody
    public void increaseviewstatus(Long callboardid){
        CallBoard callBoard=callBoardService.selectCallBoardById(callboardid);
        if (callBoard!=null){
            SysUser sysUser=(SysUser) SecurityUtils.getSubject().getPrincipal();
            ViewStatus viewStatuss=viewStatusService.selectViewStatus(sysUser.getUserName(),callboardid);
            if (viewStatuss==null){
                callBoard.setViewCount(callBoard.getViewCount()+1);
                callBoardService.updateCallBoard(callBoard);
                ViewStatus viewStatus=new ViewStatus();
                viewStatus.setUsername(sysUser.getUserName());
                viewStatus.setCallboardid(callboardid);
                viewStatusService.insertViewStatus(viewStatus);}
        }
    }

    /**
     * 修改公告板管理
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        CallBoard callBoard = callBoardService.selectCallBoardById(id);
        mmap.put("callBoard", callBoard);
        return prefix + "edit";
    }

    /**
     * 修改保存公告板管理
     */
    @Log(title = "公告板管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CallBoard callBoard)
    {
        SimpleDateFormat sdf=new SimpleDateFormat("YYYY年MM月dd日");
        String date=sdf.format(new Date());
        callBoard.setSticktime(date);
        return toAjax(callBoardService.updateCallBoard(callBoard));
    }

    /**
     * 删除公告板管理
     */
    @Log(title = "公告板管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(callBoardService.deleteCallBoardByIds(ids));
    }
}
